#include "StdAfx.h"
#include "SpriteCanvas.h"


CSpriteCanvas::CSpriteCanvas(void)
{
}


CSpriteCanvas::~CSpriteCanvas(void)
{
}

void CSpriteCanvas::AddSprite(CSprite *pSprite) 
{
	m_listSprite.push_back(pSprite);
}

void CSpriteCanvas::Tick()
{
	int nCount = 0 ; 
	DWORD dwTickCount = GetTickCount() ; 
	DWORD dwElapseTime = dwTickCount - m_dwTickCount;
	m_dwTickCount = dwTickCount;
	if(dwElapseTime <= 0)
		return;
	for(list<CSprite*>::iterator it = m_listSprite.begin(); it != m_listSprite.end(); it++)
	{
		CSprite *pSprite = *it;
		if(pSprite->IsPause())
			continue;
		pSprite->m_nElapseTime += dwElapseTime;
		if(pSprite->m_nElapseTime >= pSprite->m_nIntervalTime)
		{
			pSprite->m_nElapseTime -= pSprite->m_nIntervalTime;
			AddDirtyRect(pSprite->GetRect());
			pSprite->OnNextFrame();
			AddDirtyRect(pSprite->GetRect());
			UpdateLayer(Canval_Layer_Sprite);
			if(!pSprite->IsMove())
			{
				it = m_listSprite.erase(it);
				OnRemoveSprite(pSprite);
				continue;
			}
		}
	}
	CCanvas::Tick();
}

void CSpriteCanvas::RenderSprite(CDC *pDC)
{
	for(list<CSprite*>::iterator it = m_listSprite.begin(); it != m_listSprite.end();it++)
	{
		CSprite *pSprite = *it;
		pSprite->Render(pDC);
	}
}
